using System.Diagnostics.CodeAnalysis;
using System.Reflection;

namespace RSCore.Files;

public static class FileExtensions
{
	public static string GetText(this JsonValue json, string key)
	{
		return json.ContainsKey(key) ? json[key].Qs() : string.Empty;
	}

	/// <summary>
	/// 根据 <seealso cref="EntryType" /> 获取文件类型
	/// </summary>
	/// <param name="entry"> </param>
	/// <returns> </returns>
	/// <exception cref="ArgumentException"> </exception>
	public static Type GetFileType(this EntryType entry) => entry switch
	{
		EntryType.None => typeof(TextFile),
		EntryType.Item => typeof(ItemFile),
		EntryType.Projectile or EntryType.NPC or EntryType.Prefix or
		EntryType.DamageClass or EntryType.InfoDisplay or EntryType.Biome => typeof(SimpleFile),
		EntryType.Buff => typeof(BuffFile),
		EntryType.Tile => typeof(TileFile),
		EntryType.TownNPCMood => typeof(TownNPCFile),
		_ => throw new ArgumentException($"Invalid EntryType {entry}")
	};

	/// <summary>
	/// 判断一个 <seealso cref="Type" /> 是否拥有某属性
	/// </summary>
	/// <param name="type"> </param>
	/// <param name="property"> </param>
	/// <returns> </returns>
	public static bool HasProperty(this Type type, string property)
	{
		// 判断是否拥有属性
		return type.GetProperty(property) != null;
	}

	/// <summary>
	/// 判断一个 <seealso cref="Type" /> 是否拥有某属性，并返回该属性
	/// </summary>
	/// <param name="type"> </param>
	/// <param name="property"> </param>
	/// <param name="info"> </param>
	/// <returns> </returns>
	public static bool HasProperty(this Type type, string property, [NotNullWhen(true)] out PropertyInfo? info)
	{
		info = type.GetProperty(property);
		return info != null;
	}
}